package com.chuanjiabao.wisdom.adapter;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.databinding.DataBindingUtil;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.chuanjiabao.wisdom.R;
import com.chuanjiabao.wisdom.application.FApplication;
import com.chuanjiabao.wisdom.bean.UpdateFileTeam;
import com.chuanjiabao.wisdom.databinding.ItemUpdateFileBinding;
import com.chuanjiabao.wisdom.greendao.gen.UpdateFileTeamDao;
import com.chuanjiabao.wisdom.listener.OnBHClickListener;
import com.chuanjiabao.wisdom.util.GlideUtil;
import com.chuanjiabao.wisdom.util.dialog.HintDeleteDialog;

import org.jetbrains.annotations.NotNull;

import java.util.List;

public class UpdateFileAdapter extends RecyclerView.Adapter<BaseRecyclerViewHolder> {

    private List<UpdateFileTeam> strings;
    private Context mcontext;
    private UpdateFileTeamDao teamDao = FApplication.getInstance().getDaoSession().getUpdateFileTeamDao();

    public UpdateFileAdapter(Context context, List<UpdateFileTeam> strings) {
        this.mcontext = context;
        this.strings = strings;
    }

    @NotNull
    @Override
    public BaseRecyclerViewHolder onCreateViewHolder(@NotNull ViewGroup parent, int viewType) {
        ItemUpdateFileBinding binding = DataBindingUtil.inflate(
                LayoutInflater.from(mcontext),
                R.layout.item_update_file,
                parent,
                false
        );
        binding.setAdapter(this);
        return new TourViewHolder(binding);
    }

    @SuppressLint("SetTextI18n")
    @Override
    public void onBindViewHolder(final BaseRecyclerViewHolder holder, final int position) {
        //noinspection unchecked
        holder.bindData(strings.get(position));
        final TourViewHolder tv = (TourViewHolder) holder;
        UpdateFileTeam item = strings.get(position);

        String filePath = item.getFileName();
        GlideUtil.setLocalMethod(mcontext, filePath, tv.ivModule);

        int index = filePath.lastIndexOf("/");
        tv.tvModuleName.setText(filePath.substring(index + 1));

        long total = Long.parseLong(item.getAllSize());
        tv.tvModuleTotal.setText(GlideUtil.getFormatSize(total));

        long current = Long.parseLong(item.getSecond());
        long download = Long.parseLong(item.getDownloadSize());
        if (current > download) {
            tv.tvModuleSpeed.setText(GlideUtil.getFormatSize(current - download) + "/S");
        } else {
            if (TextUtils.equals("start", item.getStatus())) {
                tv.tvModuleSpeed.setText("0B/S");
            } else tv.tvModuleSpeed.setText("");
        }
        item.setDownloadSize(item.getSecond());

        int percent = Integer.valueOf(item.getCurrentSize());
        switch (item.getStatus()) {
            case "ready":
                tv.pBModule.setProgress(0);
                tv.tvModuleStatus.setText("等待上传");
                tv.tvModulePercent.setText("");
                tv.ivModuleStart.setVisibility(View.GONE);
                break;
            case "start":
                if (percent < 100) {
                    tv.pBModule.setProgress(percent);
                    tv.tvModuleStatus.setText("上传中...");
                    tv.tvModulePercent.setText(percent + "%");
                } else {
                    tv.pBModule.setProgress(100);
                    tv.tvModuleStatus.setText("上传完成");
                    tv.tvModulePercent.setText("100%");
                }
                tv.ivModuleStart.setVisibility(View.VISIBLE);
                tv.ivModuleStart.setImageResource(R.mipmap.icon_upload_stop);
                break;
            case "stop":
                tv.pBModule.setProgress(percent);
                tv.tvModuleStatus.setText("暂停上传");
                tv.tvModulePercent.setText(percent + "%");
                tv.ivModuleStart.setVisibility(View.VISIBLE);
                tv.ivModuleStart.setImageResource(R.mipmap.icon_upload_start);
                break;
            case "resume":
                tv.pBModule.setProgress(percent);
                tv.tvModuleStatus.setText("等待上传");
                tv.tvModulePercent.setText(percent + "%");
                tv.ivModuleStart.setVisibility(View.GONE);
                break;
        }
    }

    @Override
    public int getItemCount() {
        return strings.size();
    }

    public class TourViewHolder extends BaseRecyclerViewHolder<UpdateFileTeam> {
        ItemUpdateFileBinding functionBinding;
        ImageView ivModule, ivModuleStart;
        TextView tvModuleName, tvModuleTotal, tvModulePercent, tvModuleSpeed, tvModuleStatus;
        ProgressBar pBModule;

        public TourViewHolder(ItemUpdateFileBinding dataBinding) {
            super(dataBinding.getRoot());
            functionBinding = dataBinding;
            ivModule = dataBinding.ivModule;
            pBModule = dataBinding.pBModule;
            tvModuleStatus = dataBinding.tvModuleStatus;
            tvModuleName = dataBinding.tvModuleName;
            tvModuleTotal = dataBinding.tvModuleTotal;
            tvModulePercent = dataBinding.tvModulePercent;
            tvModuleSpeed = dataBinding.tvModuleSpeed;
            ivModuleStart = dataBinding.ivModuleStart;
        }

        @Override
        public void bindData(UpdateFileTeam data) {
            functionBinding.setBean(data);
        }
    }

    @Override
    public int getItemViewType(int position) {
        return strings.size();
    }

    public void startOrStop(View view, final UpdateFileTeam bean) {
        switch (bean.getStatus()) {
            case "start":
                sentBroadcastDelete("2", bean.getThird(), bean.getForth());
                break;
            case "stop":
                bean.setStatus("resume");
                teamDao.update(bean);
                sentBroadcastDelete("3", bean.getThird(), bean.getForth());
                break;
        }
    }

    public void deleteTeam(View view, final UpdateFileTeam bean) {
        new HintDeleteDialog(mcontext)
                .setTitle("取消上传")
                .setContent("是否要取消上传？")
                .setYes("确认取消")
                .setOnBHClickListener(new OnBHClickListener() {
                    @Override
                    public void onConfirmClick() {
                        if (TextUtils.equals("start", bean.getStatus())) {
                            sentBroadcastDelete("1", bean.getThird(), bean.getForth());
                        }
                        teamDao.delete(bean);
                    }
                })
                .show();
    }

    private void sentBroadcastDelete(String status, String uploadId, String objectName) {
        Intent intentDelete = new Intent();
        intentDelete.setAction("MODULE_ABOUT_RELATION");           //广播频道
        intentDelete.putExtra("module_funcation", status);  //广播中携带的key 和对应的value
        intentDelete.putExtra("module_upload", uploadId);   //广播中携带的key 和对应的value
        intentDelete.putExtra("module_object", objectName); //广播中携带的key 和对应的value
        mcontext.sendBroadcast(intentDelete);
    }

}